package com.ap.core.validation.message.rules;

import org.joda.time.LocalDateTime;

import com.ap.core.common.types.MessageStatus;
import com.ap.core.cxf.jaxb.paymentdetails.TxnStatusType;
import com.ap.core.dao.OrderPaymentDao;
import com.ap.core.domain.AccountNumber;
import com.ap.core.domain.PaymentConfirmationUpdateObj;
import com.ap.core.domain.RollNumber;
import com.ap.core.domain.SortCode;
import com.ap.core.message.managepayment.info.ConfirmPaymentRequestMessageInfo;
import com.ap.core.validation.message.info.MessageValidationInfo;
import com.ap.core.validation.message.info.RTPOriginalInfoForConfirmPayment;
import com.ap.core.validation.rules.AbstractValidationRule;

/**
 * User:Rohit.Dixit
 * Check if Order Payment is expired
 */
public class TV0003Rule extends AbstractValidationRule<MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment>> {

	private OrderPaymentDao orderPaymentDao;

	

    @Override
    public boolean validate(MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment> infoObject) {
        if(new LocalDateTime().isAfter(infoObject.getOriginalInfo().getConfirmationExpiryTimeStmp())){
            return false;
        }
        return true;
    }
    
    @Override
    public void onFailureAction(MessageValidationInfo<ConfirmPaymentRequestMessageInfo, RTPOriginalInfoForConfirmPayment> infoObject) {    	
    	if(new LocalDateTime().isBefore((infoObject.getOriginalInfo().getReconCutoffEndTimeStmp()))){
    		final PaymentConfirmationUpdateObj paymentConfObj = new PaymentConfirmationUpdateObj();
            paymentConfObj.setAccountNumber(AccountNumber.valueOf(infoObject.getBoundObject().getConsumerAcct().getAcctNum()));
            paymentConfObj.setFasterPaymentRef(infoObject.getBoundObject().getPaymtDetails().getFasterPaymentsRef());
            paymentConfObj.setTransactionStatus(infoObject.getBoundObject().getPaymtDetails().getPaymentStatusInfo().getTxnStatus());
            paymentConfObj.setTransactionStatusCode(infoObject.getBoundObject().getPaymtDetails().getPaymentStatusInfo().getTxnStatusCode());
            paymentConfObj.setSortCode(SortCode.valueOf(infoObject.getBoundObject().getConsumerAcct().getAcctSortCode()));
            paymentConfObj.setPaymentDescTimeStamp(infoObject.getBoundObject().getPaymtDetails().getPaymentStatusInfo().getPymtDecTimestamp());
        	if(null != infoObject.getBoundObject().getConsumerAcct().getRollNum()){
        		paymentConfObj.setRollNumber(RollNumber.valueOf(infoObject.getBoundObject().getConsumerAcct().getRollNum()));
        	}
        	paymentConfObj.setApMessageId(infoObject.getOriginalInfo().getApMessageId());
        	
        	MessageStatus messageStatus;
        	if(paymentConfObj.getTransactionStatus() == TxnStatusType.AUTHORISED){
        		messageStatus = MessageStatus.AUTHORISED;
        	}else{
        		messageStatus = MessageStatus.DECLINED;
        	}

            orderPaymentDao.updateOrderPaymentOnConfirmation(infoObject.getOriginalInfo().getApTransactionId(),messageStatus, paymentConfObj);
        }
    	
    }
    
    public void setOrderPaymentDao(OrderPaymentDao orderPaymentDao) {
        this.orderPaymentDao = orderPaymentDao;
    }

    


}
